<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/10
 * Time: 11:10
 */

namespace app\admin\controller;


use app\admin\model\MemberUser;
use app\admin\model\ShopCategory;
use app\admin\model\ShopFoods;
use app\admin\model\ShopTable;
use think\Db;
use think\facade\Cache;
use app\admin\model\ShopList;
use think\facade\Session;

class ShopController extends BaseController
{

    public function shopList(){  
    	//店铺数据信
    	$time=array();
        $shopData=ShopList::order('sort asc')->paginate(10)->each(function($item,$key){
        	$business_hours=$item->business_hours;
        	$timell=$this->shopTimeRestore($business_hours);
        	$item->business_hours=$timell;
        });
        //dump($shopData);die();
        $this->assign('shopData',$shopData);
        //数据总页数
        $page = $shopData->render();
        $this->assign('page',$page);
        //数据总个数
        $count = $shopData->total();
        $this->assign('count',$count);
        return $this->fetch();
    }

    public function shopAdd(){
        return $this->fetch();
    }

    public function shopInfo(){
        $where = Session::get('shop_id')?['id'=>Session::get('shop_id')]:'';
        $data['list'] = Db::name('shop_list')->where($where)->field('id,shop_name')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function shopData(){
    	$shop_id = input('post.shop_id');
    	$where['id']=$shop_id;
    	$shopData=ShopList::where($where)->find();
    	if($shopData['business_hours']){
    		$time=$this->shopTimeRestore($shopData['business_hours']);
    		$timearr=explode("-",$time);
    	}else{
    		$timearr[0]='';
    		$timearr[1]='';
    	}
    	$data['id']=$shopData['id'];
    	$data['shop_name']=$shopData['shop_name'];
    	$data['shop_admin']=$shopData['shop_admin'];
    	$data['phone']=$shopData['phone'];
    	$data['shop_introduce']=$shopData['shop_introduce'];
    	$data['shop_image']=$shopData['shop_image'];
    	$data['shop_ad_image']=$shopData['shop_ad_image'];
    	$data['address']=$shopData['address'];
    	$data['longitude']=$shopData['longitude'];
    	$data['latitude']=$shopData['latitude'];
    	$data['sort']=$shopData['sort'];
    	$data['status']=$shopData['status'];
    	$data['start']=$timearr[0];
    	$data['end']=$timearr[1];
    	$data['notice']=$shopData['notice'];
    	return json($data);
    }

    public function save(){
        $post = input('post.');
        $starttime=$post['start'];
        $endtime=$post['end'];
        $business_hours=$this->shopTime($starttime,$endtime);
        if(isset($post['shop_status'])){
        	$data['status']=$post['shop_status'];
        }else{
        	$data['status']=0;
        }
        $data['shop_name']=$post['shop_name'];
        $data['shop_admin']=$post['shop_admin'];
        $data['shop_introduce']=$post['shop_introduce'];
        $data['shop_image']=$post['shop_image'];
        $data['shop_ad_image']=$post['ad_image'];
        $data['longitude']=$post['longitude'];
        $data['latitude']=$post['latitude'];
        $data['address']=$post['address'];
        $data['phone']=$post['phone'];
        $data['business_hours']=$business_hours;
        $data['notice']=$post['desc'];
        $data['sort']=$post['sort'];
        $wherename['shop_name']=$post['shop_name'];
        $shopname=ShopList::where($wherename)->find();
        if($shopname){
        	$res=[
        	    'code'=>2,
        	    'msg'=>'名字重复了'
        	];
        	return json($res);
        }else{
        	$saveres=ShopList::insert($data);
        	if($saveres){
        		$res=[
	        	    'code'=>1,
	        	    'msg'=>'保存成功'
	        	];
	        	return json($res);
	        }else{
	        	$res=[
	        	    'code'=>0,
	        	    'msg'=>'保存失败'
	        	];
	        	return json($res);
	        }
        }
    }
    public function edit(){
        $post = input('post.');
        $starttime=$post['start'];
        $endtime=$post['end'];
        $business_hours=$this->shopTime($starttime,$endtime);
        if(isset($post['shop_status'])){
        	$data['status']=$post['shop_status'];
        }else{
        	$data['status']=0;
        }
        $whereid['id']=input('param.id');
        $data['shop_name']=$post['shop_name'];
        $data['shop_admin']=$post['shop_admin'];
        $data['shop_introduce']=$post['shop_introduce'];
        $data['shop_image']=$post['shop_image'];
        $data['shop_ad_image']=$post['ad_image'];
        $data['longitude']=$post['longitude'];
        $data['latitude']=$post['latitude'];
        $data['address']=$post['address'];
        $data['phone']=$post['phone'];
        $data['business_hours']=$business_hours;
        $data['notice']=$post['desc'];
        $data['sort']=$post['sort'];
        $saveres=ShopList::where($whereid)->update($data);
        if($saveres){
        	return true;
        }else{
        	return false;
        }
    }
    public function shopDel(){
        $data = input('post.id');
        $list = $this->del('shop_list','id',$data);
        if ($list){
            return json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }
    }
    public function shopImage(){
        $file =request()->file('file');
        $ret = $this->upload($file);
        return json($ret);
    }

    public function tableList(){
        $shop_id = Session::get('shop_id')?['id'=>Session::get('shop_id')]:'';
        $data = ShopList::where($shop_id)->field('id,shop_name')->select()->toArray();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function tableData($page,$limit){
        $shop_id = Session::get('shop_id')?['shop_id'=>Session::get('shop_id')]:'';
        $data = ShopTable::where($shop_id)->order('customer_code asc')->page($page,$limit)->select()->toArray();
        $count = ShopTable::where($shop_id)->count();
        foreach ($data as $k=>$v){
            $ret = ShopList::where('id',$v['shop_id'])->field('id,shop_name')->find();
            $data[$k]['shop_id'] = $ret['shop_name'];
            if ($v['waiter_id']){
                $ret = MemberUser::where('id',$v['waiter_id'])->field('id,true_name')->find();
                $data[$k]['waiter_id'] = $ret['true_name'];
            }else{
                $data[$k]['waiter_id'] = '未绑定';
            }
        }
        $res = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($res);
    }

    public function tableAdd(){
        $table_no = trim(input('post.table_no'));
        $shop_id = input('post.shop_id');
        if (!$table_no){
            return json(['code'=>0,'msg'=>'桌号不能为0']);
        }
        $insert = [
            'shop_id'=>$shop_id,
            'table_no'=>$table_no,
            'status'=>1
        ];
        $ret = ShopTable::where('table_no',$table_no)->where('shop_id',$shop_id)->find();
        if ($ret){
            return json(['code'=>0,'msg'=>'桌号已存在']);
        }
        $ret = ShopTable::create($insert);
        if (!$ret){
            return json(['code'=>0,'msg'=>'新增失败']);
        }
        return json(['code'=>1,'msg'=>'新增成功']);
    }

    public function tableDel(){
        $table_id = input('post.id');
        $ret = ShopTable::destroy(['id',$table_id]);
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }


    //生成餐桌二维码
    public function customerCode(){
        $table_id = (int)input('post.table_id');
        $table = ShopTable::get($table_id);
        $access_token = Cache::store('redis')->get('access_token');
        $file = '/code/'.$table->shop_id.$table->table_no.'customer'.'.png';
        $savePath = '../public'.$file;
        $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;
        $post = [
            'scene'=>$table->shop_id.','.$table->table_no,
            'page' =>'pages/store/store'
        ];
        $result = $this->postCurl($url,$post);
        $files = fopen($savePath,"w");//打开文件准备写入
        fwrite($files,$result);//写入
        fclose($files);
        $table->customer_code = $file;
        $ret = $table->save();
        if (!$ret) return json(['code'=>0,'msg'=>'保存二维码失败']);
        return json(['code'=>1,'msg'=>'生成二维码成功']);
    }



    public function tableStatus(){
        $table_id = (int)input('post.table_id');
        $table = ShopTable::get($table_id);
        if ($table->status == 1){
            $table->status = 0;
        }else{
            $table->status = 1;
        }
        $ret = $table->save();
        if (!$ret){
            return json(['code'=>0,'msg'=>'修改失败']);
        }
        return json(['code'=>1,'msg'=>'修改成功']);
    }


    public function spike(){

        $shop_id = Session::get('shop_id')?['id'=>Session::get('shop_id')]:'';
        $shop = ShopList::where($shop_id)->field('id,shop_name')->select()->toArray();
        $this->assign('data',$shop);
        return $this->fetch();
    }

    public function spikeData($page,$limit){
        $shop_id = Session::get('shop_id')?['shop_id'=>Session::get('shop_id')]:"";
        $where = input('param.food_name')?[['food_name','like','%'.input('param.food_name').'%']]:'';
        $data = ShopFoods::where($shop_id)->where($where)->where('spike_status','<>',0)
            ->page($page,$limit)->select()->each(function ($item){
            $shop = ShopList::where('id',$item->shop_id)->value('shop_name');
            if ($shop){
                $item->shop_id = $shop;
            }else{
                $item->shop_id = '';
            }
            $category = ShopCategory::where('id',$item->category_id)->value('category_name');
            if ($category){
                $item->category_id = $category;
            }else{
                $item->category_id = '';
            }
            $item->cook_id = MemberUser::where('id',$item->cook_id)->value('true_name');
        })->toArray();
        $count = ShopFoods::where($shop_id)->where($where)->where('spike_status','<>',0)->count();
        $res = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($res);
    }


    public function spikeAdd(){
        $shop_id = Session::get('shop_id')?['id'=>Session::get('shop_id')]:'';
        $data['shop'] = ShopList::where($shop_id)->where('status',1)->field('id,shop_name')->select()->toArray();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function spikeCategory(){
        $shop_id = input('post.shop_id');
        $data = ShopCategory::where('shop_id',$shop_id)
            ->where('spike',1)->field('id,category_name')->select()->toArray();
        return json($data);
    }

    public function spikeFood(){
        $shop_id = input('post.shop_id');
        $data = ShopFoods::where('shop_id',$shop_id)
            ->where('spike_status',0)->field('id,food_name')->select()->toArray();
        return json($data);
    }

    public function spikeAddSave(){
        $post = input('post.');
        $data = [
            'category_id'=>$post['category_id'],
            'start_time'=>strtotime($post['start']),
            'end_time' =>strtotime($post['end']),
            'spike_price'=>$post['spike_price'],
            'stock'=>$post['stock'],
            'spike_status'=>1
        ];
        $ret = ShopFoods::update($data,['id'=>$post['food_id']]);
        if (!$ret){
            return json(['code'=>0,'msg'=>'保存失败']);
        }
        return json(['code'=>1,'msg'=>'保存成功']);
    }

    public function spikeRestart(){
        $food_id = input('get.id');
        $data = ShopFoods::where('id',$food_id)
            ->field('id,start_time,end_time,spike_price,stock')->find()->toArray();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function spikeRestartSave(){
        $post = input('post.');
        $data = [
            'id'=>$post['food_id'],
            'start_time'=>strtotime($post['start']),
            'end_time'=>strtotime($post['end']),
            'spike_price'=>$post['spike_price'],
            'stock'=>$post['stock']
        ];
        $ret = ShopFoods::where($data)->find();
        if ($ret){
            return json(['code'=>0,'msg'=>'你什么都没修改']);
        }
        unset($data['id']);
        $data['spike_status'] = 1;
        $data['lock'] = 0;
        $ret = ShopFoods::update($data,['id'=>$post['food_id']]);
        if (!$ret){
            return json(['code'=>0,'msg'=>'保存失败']);
        }
        return json(['code'=>1,'msg'=>'保存成功']);
    }

    public function spikeDelay(){
        $food_id = input('get.id');
        $data = ShopFoods::where('id',$food_id)
            ->field('id,end_time,stock')->find()->toArray();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function spikeDelaySave(){
        $post = input('post.');
        $data = [
            'end_time'=>strtotime($post['end']),
            'stock'=>$post['stock']
        ];
        $ret = ShopFoods::update($data,['id'=>$post['food_id']]);
        if (!$ret){
            return json(['code'=>0,'msg'=>'保存失败']);
        }
        return json(['code'=>1,'msg'=>'保存成功']);
    }


    public function spikeDel(){
        $food_id = input('post.id');
        $food = ShopFoods::where('id',$food_id)->find();
        $ret = ShopFoods::update(['category_id'=>$food['spare_id'],'spike_status'=>0],['id'=>$food['id']]);
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }



}